//
//  RegisterViewController.m
//  reSearchDemo
//
//  Created by 云库 on 15/10/21.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "RegisterViewController.h"
#import "YunTypeVC.h"
#import "TextInput.h"

@interface RegisterViewController ()<UITextFieldDelegate,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scroll;
@property (weak, nonatomic) IBOutlet UIButton *btn;
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet KTextField *account;
@property (weak, nonatomic) IBOutlet KTextField *name;
@property (weak, nonatomic) IBOutlet KTextField *emil;
@property (weak, nonatomic) IBOutlet KTextField *password;
@property (weak, nonatomic) IBOutlet KTextField *nextPassword;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;

@end

@implementation RegisterViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.userInteractionEnabled=YES;
    self.title=@"注册云平台";
    
    self.account.delegate=self;

    

    //滚动视图
    self.scroll.userInteractionEnabled=YES;
    
    self.scroll.contentSize=CGSizeMake(self.view.bounds.size.width,self.view.bounds.size.height+50);
    
   
    self.scroll.delegate=self;
    
    //输入框
    [self.account infoStyle];
    [self.name infoStyle];
    [self.emil infoStyle];
    [self.password infoStyle];
    [self.nextPassword infoStyle];
    self.account.delegate=self;
    self.name.delegate=self;
    self.emil.delegate=self;
    self.password.delegate=self;
    self.nextPassword.delegate=self;

    //同意按钮
    self.btn.selected = NO;
    self.btn.userInteractionEnabled=YES;
    [self.btn setImage:[UIImage imageNamed:@"btn_radio_n@2x"] forState:UIControlStateNormal];
    [self.btn setImage:[UIImage imageNamed:@"btn_radio_s@2x"] forState:UIControlStateSelected];
    [self.btn addTarget:self action:@selector(selectBtnAction:) forControlEvents:UIControlEventTouchUpInside];

    //下一步按钮
    self.nextBtn.tag=1000;
    self.nextBtn.userInteractionEnabled=YES;
    [self.nextBtn addTarget:self action:@selector(nextBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self selectBtnAction:self.btn];
    
    NSURL *nsurl =[NSURL URLWithString:[NSString stringWithFormat:@"%@user/apiother/publicregist", KBSSDKAPIDomain]];
    [self.webView loadRequest:[NSURLRequest requestWithURL:nsurl]];
}

#pragma mark -- 限制输入框字数处理方法 --
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
   
    NSString *text = textField.text;
    NSInteger count = text.length + string.length;
    if (textField == self.account && count > 10) {
        return NO;
    }
    
    if ((textField == self.nextPassword || textField == self.password) && count > 15) {
        return NO;
    }
    return YES;
}


#pragma mark -- 同意按钮处理方法 --
-(void)selectBtnAction:(UIButton *)btn{
    UIButton *nextbtn = (UIButton *)[self.view viewWithTag:1000];
    if (btn.selected) {
        btn.selected = NO;
        nextbtn.selected = NO;
        nextbtn.backgroundColor = [UIColor lightGrayColor];
    }else {
        btn.selected = YES;
        nextbtn.selected = YES;
        nextbtn.backgroundColor = [UIColor blueColor];
        [nextbtn setTintColor:[UIColor clearColor]];
    }
    
}

#pragma mark -- 下一步按钮响应方法 --
-(void)nextBtnAction:(UIButton *)sender {
    [self.account resignFirstResponder];
    [self.name resignFirstResponder];
    [self.emil resignFirstResponder];
    [self.password resignFirstResponder];
    [self.nextPassword resignFirstResponder];
    
    NSString *accountText = [self.account text];
    NSString *nameText = [self.name text];
    NSString *emilText = [self.emil text];
    NSString *passwordText = [self.password text];
    NSString *nextPswText = [self.nextPassword text];

    
    if (![self validAccount:accountText]) {
        [self.account shakeAlert];
        [self showText:@"账号必须4-10个字母、数字和下划线组成"];
    } else if (nameText.length == 0) {
        [self.name shakeAlert];
    } else if (![self validEmail:emilText]) {
        [self.emil shakeAlert];
        [self showText:@"你输入的邮箱格式不对"];
    } else if (![self validPassword:passwordText]) {
        [self.password shakeAlert];
        [self showText:@"请输入6-15位密码"];
    } else if (![self validPassword:nextPswText]) {
        [self.nextPassword shakeAlert];
        [self showText:@"请输入6-15位密码"];
    } else if (![passwordText isEqualToString:nextPswText]) {
        [self.password shakeAlert];
        [self.nextPassword shakeAlert];
        [self showText:@"你输入的两密码不配"];
    } else {
        YunTypeVC * ytvc = [[YunTypeVC alloc] initWithAccount:accountText name:nameText email:emilText password:passwordText];
        [self pushViewController:ytvc];
    }
    
}

- (BOOL) validAccount:(NSString *)account {
    NSString *regex = @"^(?![0-9]+$)[0-9A-Za-z]{4,10}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:account];
}

- (BOOL) validEmail:(NSString *)email {
    NSString *regex = @"^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{1,}";
//    NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:email];
}

- (BOOL) validPassword:(NSString *)psw {
    NSString *regex = @"^[a-zA-Z0-9]{6,15}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:psw];
}

#pragma mark --textFieldDelegate--
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
   
    if (self.view.frame.size.height<680) {
        self.scroll.contentOffset=CGPointMake(0, textField.frame.origin.y-150);
    }
    
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
//    [textField resignFirstResponder];
//    [self.scroll endEditing:YES];
    if (textField==self.account) {
        [self.account resignFirstResponder];
        [self.name becomeFirstResponder];
    }
    else if (textField==self.name) {
        [self.name resignFirstResponder];
        [self.emil becomeFirstResponder];
    }
    else if (textField==self.emil) {
        [self.emil resignFirstResponder];
        [self.password becomeFirstResponder];
    }
    else if (textField==self.password){
        [self.password resignFirstResponder];
        [self.nextPassword becomeFirstResponder];
    }
    else if (textField==self.nextPassword) {
        
        [self nextBtnAction:self.nextBtn];
    }
    
    
 
    return YES;
}


//点击输入框外回收键盘
//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//    [self.view endEditing:YES];
//    
//    [self.scroll resignFirstResponder];
//    [self.account resignFirstResponder];
//    [self.name resignFirstResponder];
//    [self.emil resignFirstResponder];
//    [self.password resignFirstResponder];
//    [self.nextPassword resignFirstResponder];
//}

#pragma mark---scrollview代理方法
//滑动回收键盘
//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
//{
//    DLog(@"scrollViewWillBeginDecelerating");
//    [self.scroll resignFirstResponder];
//    [self.account resignFirstResponder];
//    [self.name resignFirstResponder];
//    [self.emil resignFirstResponder];
//    [self.password resignFirstResponder];
//    [self.nextPassword resignFirstResponder];
//
//}

//拖动视图回收键盘
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.scroll resignFirstResponder];
    [self.account resignFirstResponder];
    [self.name resignFirstResponder];
    [self.emil resignFirstResponder];
    [self.password resignFirstResponder];
    [self.nextPassword resignFirstResponder];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
